package org.szy.core.common.entity;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import org.noear.solon.Solon;
import org.szy.core.common.prop.Sm2Properties;
import org.szy.core.common.uilts.CryptogramUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回类
 *
 * @param <T>
 * @author wangbuhao
 */
public class R<T> implements Serializable {

    public final static int OK_CODE;
    public final static String OK_MSG;
    public final static int FAIL_CODE;
    public final static String FAIL_MSG;
    private static final long serialVersionUID = -2225374287899397190L;

    // 加密
    private static final boolean encrypt = Sm2Properties.enabled;

    static {
        OK_CODE = 200;
        OK_MSG = "操作成功";
        FAIL_CODE = HttpStatus.HTTP_INTERNAL_ERROR;
        FAIL_MSG = "服务器繁忙，稍后再试";
    }

    /**
     * 结果标识，200：代表成功，500：代表错误/失败
     */
    private int code = 500;
    /**
     * 结果描述
     */
    private String msg;
    /**
     * 数据信息
     */
    private T data;
    /**
     * 时间戳(毫秒)
     */
    private long timeStamp = System.currentTimeMillis();

    /**
     * 成功
     */
    public static <T> R<T> ok() {
        return ok(OK_MSG);
    }

    public static <T> R<T> ok(String msg) {
        return ok(OK_CODE, msg);
    }

    public static <T> R<T> ok(int code, String msg) {
        return ok(code, msg, null);
    }

    public static <T> R<T> data(T data) {
        return ok(OK_CODE, OK_MSG, data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return ok(OK_CODE, msg, data);
    }

    public static <T> R<T> ok(int code, String msg, T data) {
        return restResult(code, msg, data);
    }

    /**
     * 失败
     */
    public static <T> R<T> fail() {
        return fail(FAIL_MSG);
    }

    public static <T> R<T> fail(String msg) {
        return fail(FAIL_CODE, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return fail(code, msg, null);
    }

    public static <T> R<T> fail(T data) {
        return fail(FAIL_MSG, data);
    }

    public static <T> R<T> fail(String msg, T data) {
        return fail(FAIL_CODE, msg, data);
    }

    public static <T> R<T> fail(int code, String msg, T data) {
        return restResult(code, msg, data);
    }


    public static R<?> bool(boolean update, String okMsg, String failMsg) {
        return update ? R.ok(okMsg) : R.fail(failMsg);
    }

    public static R<?> bool(boolean update) {
        return update ? R.ok() : R.fail();
    }

    /**
     * 自动判断
     */
    public static <T> R<T> asNull(T data, String okMsg, String failMsg) {
        if (ObjUtil.isNull(data)) return fail(failMsg, data);
        else return ok(okMsg, data);
    }

    public static <T> R<T> asNull(T data) {
        if (ObjUtil.isNull(data)) return fail("数据不存在", data);
        else return ok("success", data);
    }

    /**
     * 构建指定状态码
     */
    public static <T> R<T> restResult(int code, String msg, T data) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        if (encrypt && code == 200 && data != null) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("wangbuhao", data);
            String str = JSONUtil.toJsonStr(map);
            if (str != null && str.length() < 50000) {
                apiResult.setData((T) CryptogramUtil.doSm2Encrypt(str));
            } else {
                apiResult.setData(data);
            }
        } else {
            apiResult.setData(data);
        }
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }
}
